import moment from '../../../../static/common/js/moment.min.js'
const filters = {
  time: value=>{
    if(!value) {
      return '';
    }else {
      return moment(value*1000).format('YYYY-MM-DD HH:mm');
    }
  },
  fulltime: value=>{
    if(!value) {
      return '';
    }else {
      return moment(value*1000).format('YYYY-MM-DD HH:mm:ss');
    }
  },
  date: value=>{
    if(!value) {
      return '';
    }else {
      return moment(value*1000).format('YYYY/MM/DD');
    }
  },
  //千分位格式化金额
  regexMoney: value=> {
    return _regex(value,'money');
  },
  //千分位格式化金额
  regexMoneyUnit: value=> {
    return _regex(value,'unit');
  },
  //千分位格式化数字
  regexNum: value=> {
    return (value || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  }
};

function _regex(value,type) {
  var s = value;
  s = s + '';
  if(/[^0-9\.]/.test(s)) return '';
  s=s.replace(/^(\d*)$/,"$1.");
  s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
  s=s.replace(".",",");
  var re=/(\d)(\d{3},)/;
  while(re.test(s))
    s=s.replace(re,"$1,$2");
  s=s.replace(/,(\d\d)$/,".$1");
  if(type == 'money') {
    return "" + s.replace(/^\./,"0.")
  } else if(type == 'unit') {
    return s.replace(/^\./,"0.") + '元';
  }else {
    return s.replace(/^\./,"0.")
  }
}
export default filters;
